package com.base.cn.platform.os.controller.op.advert;

import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.op.advert.OpAdvert;
import com.base.cn.platform.os.service.op.advert.OpAdvertBiz;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 广告接口
 * @author ll
 * @date 2018/5/17 10:36
 */
@RestController
@RequestMapping("/manage/op")
public class OpAdvertController extends BaseController {

    @Autowired
    private OpAdvertBiz opAdvertBiz;

    /**
     * 搜索广告列表
     * @param opAdvert 广告搜索条件
     * @param currentPage 分页
     * @param courseData 课程
     * @param createUserData 创建用户
     * @param advertPlaceData 广告位置
     * @return
     */
    @RequestMapping("/queryAdvertListPage")
    public PageInfo<OpAdvert> queryAdvertListPage(@RequestBody OpAdvert opAdvert,
                                                  @RequestParam(value = "currentPage",required = false,defaultValue = "1")Integer currentPage,
                                                  @RequestParam(value = "courseData",required = false,defaultValue = "false")boolean courseData,
                                                  @RequestParam(value = "createUserData",required = false,defaultValue = "false")boolean createUserData,
                                                  @RequestParam(value = "advertPlaceData",required = false,defaultValue = "false")boolean advertPlaceData){
        Pagination page = new Pagination();
        page.setCurrentPage(currentPage);
        return opAdvertBiz.queryAdvertListPage(opAdvert,page,courseData,createUserData,advertPlaceData);
    }


    /**
     * 搜索广告列表
     * @param opAdvert 广告搜索条件
     * @param courseData 课程
     * @param createUserData 创建用户
     * @param advertPlaceData 广告位置
     * @return
     */
    @RequestMapping("/queryAdvertList")
    public List<OpAdvert> queryAdvertList(@RequestBody OpAdvert opAdvert,
                                          @RequestParam(value = "courseData",required = false,defaultValue = "false")boolean courseData,
                                          @RequestParam(value = "createUserData",required = false,defaultValue = "false")boolean createUserData,
                                          @RequestParam(value = "advertPlaceData",required = false,defaultValue = "false")boolean advertPlaceData){
        return opAdvertBiz.queryAdvertList(opAdvert,courseData,createUserData,advertPlaceData);
    }

    /**
     * 添加修改广告
     * @param opAdvert
     * @return
     */
    @RequestMapping("/saveUpdateOpAdvert")
    public Map<String,Object> saveUpdateOpAdvert(@RequestBody OpAdvert opAdvert){
        if(!ObjectUtils.isNotEmpty(opAdvert)){
            return ResultUtil.ERROR("数据错误");
        }
        if(!StringUtils.isNotEmpty(opAdvert.getPlaceKey())){
            return  ResultUtil.ERROR("请选择广告显示位置");
        }
        if(!StringUtils.isNotEmpty(opAdvert.getImageJson())){
            return ResultUtil.ERROR("请上传广告封面图片");
        }
        //判断跳转类型
        if(opAdvert.getRedirectType()==null||opAdvert.getRedirectType()>=0){
            if(opAdvert.getRedirectType()==1){
                if(!StringUtils.isNotEmpty(opAdvert.getPageUrl())){
                    return ResultUtil.ERROR("请上传广告专题文件或填写专题链接");
                }
            }else if(opAdvert.getRedirectType()==2){
                if(!DataUtil.idIsNotNull(opAdvert.getCourseId())){
                    return ResultUtil.ERROR("请选择广告跳转的课程");
                }
            }else if (opAdvert.getRedirectType()==3 && !StringUtils.isNotEmpty(opAdvert.getPageUrl())){
                return ResultUtil.ERROR("请填写广告跳转的外链地址");
            }
        }
        OpAdvert advert = opAdvertBiz.saveUpdateAdvert(opAdvert);
        return ResultUtil.SUCCESS("编辑成功",advert);
    }

    /**
     * 删除广告
     * @param ids
     * @return
     */
    @RequestMapping("/deleteAdvertByIds")
    public Map<String,Object> deleteAdvertByIds(@RequestParam("ids")String ids){
        opAdvertBiz.deleteAdvert(ids);
        return ResultUtil.SUCCESS("删除成功");
    }



    /**
     * 搜索广告
     * @param id 广告搜索条件
     * @param courseData 课程
     * @param createUserData 创建用户
     * @param advertPlaceData 广告位置
     * @return
     */
    @RequestMapping("/queryOpAdvertById")
    public OpAdvert queryOpAdvertById(@RequestParam(value = "id")BigDecimal id,
                                      @RequestParam(value = "courseData",required = false,defaultValue = "false")boolean courseData,
                                      @RequestParam(value = "createUserData",required = false,defaultValue = "false")boolean createUserData,
                                      @RequestParam(value = "advertPlaceData",required = false,defaultValue = "false")boolean advertPlaceData){
        return opAdvertBiz.queryAdvertById(id,courseData,createUserData,advertPlaceData);
    }

    /**
     * 修改广告状态
     * @param ids
     * @return
     */
    @RequestMapping("/updateOpAdvertStatus")
    public Map<String,Object> updateOpAdvertStatus(@RequestParam("ids")String ids,
                                                   @RequestParam("status")Integer status){
        opAdvertBiz.updateOpAdvertStatus(ids,status);
        String message ="";
        if (status==1){
            message="恢复成功";
        }else if (status==2){
            message="禁用成功";
        }
        return ResultUtil.SUCCESS(message);
    }
}
